<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="航班ID" prop="flightId">
        <el-input
          v-model="queryParams.flightId"
          placeholder="请输入航班ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发日期" prop="departureDate">
        <el-date-picker clearable
                        v-model="queryParams.departureDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出发日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="机票状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择机票状态" clearable>
          <el-option
            v-for="dict in dict.type.ticket_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="座位类型" prop="seatType">
        <el-select v-model="queryParams.seatType" placeholder="请选择座位类型" clearable>
          <el-option
            v-for="dict in dict.type.seat_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="座位号" prop="seatNumber">
        <el-input
          v-model="queryParams.seatNumber"
          placeholder="请输入座位号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机票价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入机票价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      </el-form-item>
      <el-form-item label="预订日期" prop="bookingDate">
        <el-date-picker clearable
                        v-model="queryParams.bookingDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择预订日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="ticketsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="机票ID" align="center" prop="ticketId"/>
      <el-table-column label="航班ID" align="center" prop="flightId"/>
      <el-table-column label="出发日期" align="center" prop="departureDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="机票状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ticket_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="座位类型" align="center" prop="seatType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.seat_type" :value="scope.row.seatType"/>
        </template>
      </el-table-column>
      <el-table-column label="座位号" align="center" prop="seatNumber"/>
      <el-table-column label="机票价格" align="center" prop="price"/>
      <el-table-column label="退款费用" align="center" prop="refundFee"/>
      <el-table-column label="改签费用" align="center" prop="changeFee"/>
      <el-table-column label="乘客ID" align="center" prop="passengerId"/>
      <el-table-column label="预订日期" align="center" prop="bookingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >退票
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="$message.warning(`改签请去在线订票页面操作`)"
          >改签
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 退票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <!--支付宝收款码-->
      <div style="text-align: center">请支付{{ this.form.refundFee }} 元</div>
      <image-preview
        src="https://ts1.cn.mm.bing.net/th/id/R-C.1e5871c9b34fc3bf72af08f3ca9cf38c?rik=izMAIOABpwShEA&riu=http%3a%2f%2fcache.mobanma.com%2fUploads%2fpro%2fcover%2f2017%2f02%2f08%2f201702082213457740FemOPA.png&ehk=c%2bvuGxzjrtyPEFLGt%2fo%2fARfphuxmo7mlhEp0x7tZshs%3d&risl=&pid=ImgRaw&r=0"
        :width="50" :height="50"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTickets, getTickets, delTickets, addTickets, updateTickets} from "@/api/airline/tickets";

export default {
  name: "Tickets",
  dicts: ['ticket_status', 'seat_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机票信息表格数据
      ticketsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flightId: null,
        departureDate: null,
        status: null,
        seatType: null,
        seatNumber: null,
        price: null,
        refundFee: null,
        changeFee: null,
        passengerId: null,
        bookingDate: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flightId: [
          {required: true, message: "航班ID不能为空", trigger: "blur"}
        ],
        departureDate: [
          {required: true, message: "出发日期不能为空", trigger: "blur"}
        ],
        price: [
          {required: true, message: "机票价格不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询机票信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.passengerId = this.$store.getters.userId;
      listTickets(this.queryParams).then(response => {
        this.ticketsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ticketId: null,
        flightId: null,
        departureDate: null,
        status: null,
        seatType: null,
        seatNumber: null,
        price: null,
        refundFee: null,
        changeFee: null,
        passengerId: null,
        bookingDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ticketId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机票信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ticketId = row.ticketId || this.ids
      getTickets(ticketId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "退票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.ticketId != null) {
        this.form.passengerId = 0
        this.form.status = 0;
        this.form.bookingDate = 0;
        updateTickets(this.form).then(response => {
          this.$modal.msgSuccess("退票成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ticketIds = row.ticketId || this.ids;
      this.$modal.confirm('是否确认删除机票信息编号为"' + ticketIds + '"的数据项？').then(function () {
        return delTickets(ticketIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('airline/tickets/export', {
        ...this.queryParams
      }, `tickets_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
